#comments-start
Copyright (c) <2011>, <John Seekins>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#comments-end
Func BrowseForWimToSave($rootSource = "C:")
	$sFile = FileSaveDialog("Select destination file", $rootSource, "WIM Files (*.wim)", 3)
	If @error Then Return ""

	If StringRight($sFile, 4) <> ".wIM" Then $sFile &= ".wim"

	Return $sFile
EndFunc   ;==>BrowseForWimToSave

Func BrowseForWimToLoad($rootSource = "C:")
	$sFile = FileOpenDialog("Select source file", $rootSource, "WIM Files (*.wim)", 3)
	If @error Then Return ""

	Return $sFile
EndFunc   ;==>BrowseForWimToLoad

Func DoCapture($captureSource, $captureDestination, $captureCompression, $captureCheck, $captureName, $captureDescription, $captureConfig)

	Local $sCmd = "imagex.exe /capture"

	If $captureCompression = "fast" Then
		$sCmd = $sCmd & " /compress fast"
	ElseIf $captureCompression = "maximum" Then
		$sCmd = $sCmd & " /compress maximum"
	EndIf

	If $captureCheck = 1 Then $sCmd &= " /boot /check /verify"

	$sCmd &= " /config " & $captureConfig & " " & $captureSource & " " & $captureDestination & ' "' & $captureName & '" "' & $captureDescription & '"'

	$ret = RunWait($sCmd, @ScriptDir, @SW_SHOW)
	If $ret <> 0 Then
		SplashTextOn("Imaging Failed!", "Imagex.exe failed with code: " & @error, 400, 200, -1, -1, 32, "Tahoma", 16, 500)
		Sleep(3000)
		SplashOff()
	EndIf
EndFunc   ;==>DoCapture

Func DoApply($applySource, $applyDestination, $applyVerify, $applyName, $applyFormat, $applyWriteBoot)

	Local $bootsecType = ""

	If (FileExists($applySource)) AND (StringRight($applySource, 4) = ".wim") Then
		If $applyFormat = 1 Then
			$sCmd = "format.com " & $applyDestination
			$sCmd &= " /Q /Y"
			$ret = RunWait($sCmd, @WorkingDir, @SW_SHOW)
			If $ret <> 0 Then
				SplashTextOn("Format Failed!", "Format.com failed with code: " & @error, 400, 200, -1, -1, 32, "Tahoma", 16, 500)
				Sleep(3000)
				SplashOff()
			EndIf
		EndIf
		$sCmd = "imagex.exe /apply"

		If $applyVerify = 1 Then $sCmd &= " /verify"

		$sCmd &= " " & $applySource & " " & $applyName & " " & $applyDestination

		$ret = RunWait($sCmd, @WorkingDir, @SW_SHOW)
		If $ret <> 0 Then
			SplashTextOn("Imaging Failed!", "Imagex.exe failed with code: " & @error, 400, 200, -1, -1, 32, "Tahoma", 16, 500)
			Sleep(3000)
			SplashOff()
		EndIf

		If $applyWriteBoot = 1 Then
			If FileExists($applyDestination & "\boot.ini") Then
				$bootsecType = "/nt52"
			Else
				$bootsecType = "/nt60"
			EndIf
			$ret = RunWait("bootsect.exe " & $bootsecType & " /force " & $applyDestination, @WindowsDir, @SW_HIDE)
			If $ret <> 0 Then
				SplashTextOn("Boot Sector writing failed!", $applyDestination & " boot sector re-write failed with code: " & @error, 400, 200, -1, -1, 32, "Tahoma", 16, 500)
				Sleep(3000)
				SplashOff()
				Return 0
			EndIf
			$ret = RunWait("bootsect.exe " & $bootsecType & " SYS /mbr", @WindowsDir, @SW_HIDE)
			If $ret <> 0 Then
				SplashTextOn("Master Boot Record writing failed!", "MBR Re-write failed with code: " & @error, 400, 200, -1, -1, 32, "Tahoma", 16, 500)
				Sleep(3000)
				SplashOff()
				Return 0
			EndIf
		EndIf
		Return 1
	Else
		MsgBox(48, "Invalid image!", "Image " & $applySource & " does not exist or is an improperly formatted .wim file!")
		Return 0
	EndIf
EndFunc   ;==>DoApply
